package com.ruoyi.project.examine.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.examine.domain.CheckMode;
import com.ruoyi.project.examine.domain.ExamCost;
import com.ruoyi.project.examine.service.CheckService;
import com.ruoyi.project.system.newfunction.domain.Exam_cost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.*;

@RequestMapping("/examine/newCheck")
@RestController
public class CheckController extends BaseController {
    @Autowired
    private CheckService checkService;
    /**
     * 科室查询
     * @return
     */
    @GetMapping("/findCheck")
    public AjaxResult findCheck(){
        List<Exam_cost> checkName = checkService.findCheckName();
        return AjaxResult.success(checkName);
    }
    /**
     * 新开检查
     * @param checkCode 检查单号
     * @return
     */
    @GetMapping("/findCheckMode")
    public AjaxResult findCheckMode(String examName,String checkCode){
        List<CheckMode> checkStart = checkService.findCheckStart(examName, checkCode);

        return AjaxResult.success(checkStart.size()!=0?checkStart.get(0):null);
    }
    /**
     * 修改检查状态
     * @param checkCode
     * @return
     */
    @GetMapping("/changeCheckStatus")
    public AjaxResult changeCheckStatus(String checkCode){
        int i = checkService.changeCheck(checkCode);
        return toAjax(i);
    }
    /**
     * 录入检查结果前的数据回显
     * @param examName
     * @param checkCode
     * @return
     */
    @GetMapping("/findCheckMode2")
    public AjaxResult findCheckMode2(String examName,String checkCode){
        List<CheckMode> result1 = checkService.findResult1(examName, checkCode);
        System.out.println(result1);
        return AjaxResult.success(result1);
    }
    /**
     * 录入检查结果
     * 并修改检查状态和添加检查图片
     * @param checkCode
     * @param checkResult
     * @return true 数据修改成功  false 数据修改失败
     */
    @GetMapping("/changeCheckStatus3")
    public AjaxResult ChangeCheckStatus2(String checkCode, String checkResult, String filename,String filesite ){
        int i=0;
        boolean change = checkService.change(checkCode, checkResult,filename,filesite);
        System.out.println(change);
        if (change==true){
            i=1;
        }
        System.out.println(i);
        return toAjax(i);
    }

    /**
     * 查看检查结果
     * @param examName
     * @param checkCode
     * @return
     */
    @GetMapping("/findCheckMode3")
    public AjaxResult findCheckMode3(String examName,String checkCode){
        List<CheckMode> checkStart = checkService.checkresult(examName, checkCode);
        System.out.println(checkStart);
        return AjaxResult.success(checkStart);
    }
    //保存文件的地址
    private final String dir="D:\\uploadFiles";
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file) {
        /*//添加图片
        //获取文件的原始名称
        String originalFilename = file.getOriginalFilename();
        //获得文件的后缀名
        String extName=originalFilename.substring(originalFilename.lastIndexOf("."));
        //系统随机命名的名称
        String randomFileName= UUID.randomUUID().toString()+extName;
        //保存文件到本地文件夹中
        File persistFile = new File(dir, randomFileName);
        file.transferTo(persistFile);
       *//* Map<String,Object> map=new HashMap<>();
        map.put("filename",randomFileName);
        map.put("filesite",dir+"\\"+randomFileName);
        System.out.println(map);*/
        String filename = checkService.upload(file);
        return AjaxResult.success(filename);
    }
    /**
     * 下载
     * 通过响应输出流，向客户端输出数据，需要定制响应头和附件信息
     * @param
     * @param
     * @param response
     *//*
    @RequestMapping("/download")

    public void download(String fileName, String originalFileName, HttpServletResponse response){
        //查询文件内容
        byte[] datas=fileUploadService.getFile(fileName);
        //设置响应头
        response.setContentType("application/anctet-stream");
        //设置附件
        response.setHeader("content-disposition","attachment;filename="+originalFileName);
        //通过响应输出流，把文件输出到客户端
        try {
            response.getOutputStream().write(datas,0,datas.length);
            response.getOutputStream().flush();
        }catch (Exception e){
            e.printStackTrace();
        }
    }*/

    @GetMapping("/findCheckMode4")
    public void download(String filename,HttpServletResponse response) throws IOException {
        /*File file=new File(new File(dir),filename);
        FileInputStream inputStream = new FileInputStream(file);
        byte[] datas = new byte[inputStream.available()];
        inputStream.read(datas,0,datas.length);
        response.setContentType("application/anctet-stream");
        response.setHeader("content-disposition","attachment;filename="+filename);
        response.getOutputStream().write(datas,0,datas.length);
        response.getOutputStream().flush();*/
        System.out.println("进入图片回显模块");
        byte[] datas = checkService.getImage(filename);
        File file = new File("E:/yjd.png");
        System.out.println(file.exists());

        if (datas != null) {
            try {
                response.setContentType("application/octet-stream");
                response.getOutputStream().write(datas, 0, datas.length);
                response.getOutputStream().flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
